/*
 *  Description:
 *  Compare speed between two Sort methods
 */
package algs4.sort;

import java.util.Random;
import algs4.basic.Stopwatch;

// import edu.princeton.cs.algs4.Quick;
// import edu.princeton.cs.algs4.QuickX;

public class SortCompare
{
    private static final Random random;

    static {
        random = new Random(System.currentTimeMillis());
    }

    public static double time(String alg, Double[] a) {
        Stopwatch timer = new Stopwatch();

        switch (alg) {
            case "Selection": Selection.sort(a); break;
            case "Insertion": Insertion.sort(a); break;
            case "Shell":     Shell.sort(a);     break;
            case "Merge":     Merge.sort(a);     break;
            case "MergeBU":   MergeBU.sort(a);   break;
            case "Quick":     Quick.sort(a);     break;
            case "Quick3way": Quick3way.sort(a); break;
            case "Heap":      Heap.sort(a);      break;
            default:                             break;
        }

        return timer.elapsedTime();
    }

    public static double timeRandomInput(String alg, int N, int T) {
        double total = 0.0;
        Double[] a = new Double[N];
        for (int t = 0; t < T; t++) {
            // Take one test(generate an array of double and sort it)
            for (int i = 0; i < N; i++)
                a[i] = random.nextDouble();
            total += time(alg, a);
        }
        return total;
    }

    public static void main(String[] args) {
        String alg1 = args[0];
        String alg2 = args[1];
        int N       = Integer.parseInt(args[2]);
        int T       = Integer.parseInt(args[3]);
        double t1   = timeRandomInput(alg1, N, T);
        double t2   = timeRandomInput(alg2, N, T);

        System.out.printf("For %d random Doubles\n", N);
        System.out.printf("  %s is %.1f times faster than %s\n",
                          alg1, t2 / t1, alg2);
    }
}
